package com.monitor.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpClientsMaterielMapper;
import com.monitor.dao.ErpCustomerMapper;
import com.monitor.dao.ErpOrderdMapper;
import com.monitor.dao.ErpOrderdMaterielMapper;
import com.monitor.dao.ErpWarehouseMapper;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpOrderd;
import com.monitor.entity.ErpOrderdMateriel;
import com.monitor.entity.ErpWarehouse;
import com.monitor.service.ErpOrderdService;
import com.monitor.util.DataUtil;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 需求汇总表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-11
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ErpOrderdServiceImpl extends ServiceImpl<ErpOrderdMapper, ErpOrderd> implements ErpOrderdService {

    @Resource
    private ErpOrderdMapper orderdMapper;
    
    @Resource
    private ErpOrderdMaterielMapper erpOrderdMaterielMapper;
    
    @Resource
    private ErpWarehouseMapper erpWarehouseMapper;
    
    @Resource
    private ErpClientsMaterielMapper erpClientsMaterielMapper;
    
    @Resource
    private ErpCustomerMapper custMapper;

    @Override
    public List<Integer> getOrderdId(String materielName, String materielBrand, String specifications, String model) {
        return orderdMapper.getOrderdId(materielName,materielBrand,specifications,model);
    }

	@Override
	public Result<String> selectSheetId(String time) {
		// TODO Auto-generated method stub
		String shi=DataUtil.getStringDate(time);
		String id="XQJH"+shi;
		List<ErpOrderd> list=orderdMapper.selectList(new EntityWrapper<ErpOrderd>().like("sheet_id", id).orderBy("id", false));
		if(list.size()>0) {
			ErpOrderd ord=list.get(0);
			String str=ord.getSheetId().substring(12, ord.getSheetId().length());
    		str=IDUtils.addOne(str);
    		id+=str;
		}else {
			id+="00001";
		}
		return new Result<String>(id, 1);
	}

	@Override
	public Result<Integer> addOrderd(ErpOrderd ord, String json) {
		// TODO Auto-generated method stub
		try {
			List<ErpOrderdMateriel> list=JSON.parseArray(json, ErpOrderdMateriel.class);
			if(ord.getId()==null||ord.getId()==0) {
				ord.setWordState(0);
				int count=orderdMapper.insert(ord);
				int num=0;              //用于判断是否全是空数据
				for(ErpOrderdMateriel om:list) {
					if(om.getMaterielName()==null||om.getMaterielName().equals("")) {
						num++;
						continue;
					}
					om.setOrderId(ord.getId());
					om.setWorkState(0);
					erpOrderdMaterielMapper.insert(om);
				}
				if(num>=list.size()) {
					orderdMapper.deleteById(ord.getId());
				}
				return new Result<Integer>(count, 1);
			}else {
				int count=orderdMapper.updateById(ord);
				erpOrderdMaterielMapper.delete(new EntityWrapper<ErpOrderdMateriel>().eq("order_id", ord.getId()));
				for(ErpOrderdMateriel om:list) {
					om.setId(null);
					om.setOrderId(ord.getId());
					erpOrderdMaterielMapper.insert(om);
				}
				return new Result<Integer>(count, 1);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
		
	}

	@Override
	public Result<List<Map<String, Object>>> selectOrderByTime(String startTime, String endTime,int custId) {
		// TODO Auto-generated method stub
		try {
			List<Map<String, Object>> list=erpOrderdMaterielMapper.selectByTime(startTime, endTime,custId);
			return new Result<List<Map<String,Object>>>(list, list.size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<Integer> deleteOrder(int orderId) {
		// TODO Auto-generated method stub
		try {
			int count=orderdMapper.deleteById(orderId);
			erpOrderdMaterielMapper.delete(new EntityWrapper<ErpOrderdMateriel>().eq("order_id", orderId));
			return new Result<Integer>(count, count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<Map<String, Object>> selectByOrderId(int orderId) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map=new HashMap<>();
			ErpOrderd order=orderdMapper.selectById(orderId);
			List<ErpOrderdMateriel> list=erpOrderdMaterielMapper.selectList(new EntityWrapper<ErpOrderdMateriel>().eq("order_id", orderId));
			map.put("order", order);
			map.put("materielList", list);
			return new Result<Map<String,Object>>(map, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<List<Map<String, Object>>> selectByType(String startTime,String endTime,int custId) {
		// TODO Auto-generated method stub
		try {
			List<Map<String, Object>> list=erpOrderdMaterielMapper.selectByType(startTime,endTime,custId);
			for(Map<String, Object> li:list) {
				int shu=erpClientsMaterielMapper.selectCountMateriel(custId, li.get("materiel_name").toString(), li.get("materiel_brand").toString(), li.get("materiel_Specifications").toString(), li.get("materiel_model").toString(), li.get("materiel_place").toString(), li.get("materiel_state").toString(), li.get("materiel_unit").toString());
				int xu=Integer.parseInt(li.get("xu").toString());
				xu=xu-shu;
				if(xu<0) {
					xu=0;
				}
				ErpWarehouse wareHouse=new ErpWarehouse();
				wareHouse.setMaterielName(li.get("materiel_name").toString());
				wareHouse.setMaterielBrand(li.get("materiel_brand").toString());
				wareHouse.setMaterielSpecifications(li.get("materiel_Specifications").toString());
				wareHouse.setMaterielModel(li.get("materiel_model").toString());
				wareHouse.setMaterielPlace(li.get("materiel_place").toString());
				wareHouse.setMaterielState(li.get("materiel_state").toString());
				wareHouse.setMaterielUnit(li.get("materiel_unit").toString());
				ErpWarehouse ware=erpWarehouseMapper.selectWareHouse1(wareHouse);
				int kucun=0;
				int yujing=0;
				//int xu=Integer.parseInt(li.get("xu").toString());
				if(ware!=null) {
					if(ware.getMaterielNumber()!=null) {
						kucun=ware.getMaterielNumber();
					}
					if(ware.getMaterielWarnAmount()!=null) {
						yujing=ware.getMaterielWarnAmount();
					}
				}
				li.put("xu", xu);
				li.put("kucun", kucun);
				li.put("yujing", yujing);
				int caigou=xu-kucun+yujing;
				if(caigou<0) {
					caigou=0;
				}
				li.put("caigou", caigou);
			}
			return new Result<List<Map<String,Object>>>(list, list.size());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<List<Map<String, Object>>> selectByCustId(int custId) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list=erpOrderdMaterielMapper.selectByCustId(custId);
		List<Map<String, Object>> removeList=new ArrayList<>();
		for(Map<String, Object> li:list) {
			int shu=erpClientsMaterielMapper.selectCountMateriel(custId, li.get("materiel_name").toString(), li.get("materiel_brand").toString(), li.get("materiel_Specifications").toString(), li.get("materiel_model").toString(), li.get("materiel_place").toString(), li.get("materiel_state").toString(), li.get("materiel_unit").toString());
			int xu=Integer.parseInt(li.get("xu").toString());
			if(shu!=0&&xu-shu<=0) {
				removeList.add(li);
				continue;
			}else if(shu!=0&&xu-shu>0) {
				xu=xu-shu;
			}
			ErpWarehouse wareHouse=new ErpWarehouse();
			wareHouse.setMaterielName(li.get("materiel_name").toString());
			wareHouse.setMaterielBrand(li.get("materiel_brand").toString());
			wareHouse.setMaterielSpecifications(li.get("materiel_Specifications").toString());
			wareHouse.setMaterielModel(li.get("materiel_model").toString());
			wareHouse.setMaterielPlace(li.get("materiel_place").toString());
			wareHouse.setMaterielState(li.get("materiel_state").toString());
			wareHouse.setMaterielUnit(li.get("materiel_unit").toString());
			ErpWarehouse ware=erpWarehouseMapper.selectWareHouse1(wareHouse);
			int kucun=0;
			int yujing=0;
			if(ware!=null) {
				if(ware.getMaterielNumber()!=null) {
					kucun=ware.getMaterielNumber();
				}
				if(ware.getMaterielWarnAmount()!=null) {
					yujing=ware.getMaterielWarnAmount();
				}
			}
			li.put("kucun", kucun);
			li.put("yujing", yujing);
			int caigou=xu-kucun+yujing;
			li.put("caigou", caigou);
		}
		list.removeAll(removeList);
		return new Result<List<Map<String,Object>>>(list, list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> selectByOrderList(String startTime, String endTime, int custId) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list=erpOrderdMaterielMapper.selectByOrderList(startTime, endTime, custId);
		for(Map<String, Object> li:list) {
			ErpCustomer cust=custMapper.selectById(custId);
			li.put("custName", cust.getCusName());
			
		}
		return new Result<List<Map<String,Object>>>(list, list.size());
	}
}
